<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2017/8/24
 * Time: 17:20
 */

namespace app\common\model;
use app\common\lib\Data;
use traits\model\SoftDelete;


class Menu extends BaseModel
{
    protected $table = 'ng_menus';
    protected $createTime = 'created_at';
    protected $updateTime = 'updated_at';
    protected $autoWriteTimestamp = 'datetime';

    use SoftDelete;
    protected $deleteTime = 'deleted_at';

    protected $hidden = [
        'created_at',
        'updated_at'
    ];

    public function user()
    {
        return $this->belongsTo('User', 'user_id', 'id');
    }

    /**
     * 获取全部数据
     * @param string $type      tree获取树形结构 level获取层级结构
     * @param string $order     排序方式
     * @param string $name      字段名
     * @param string $child     主键id
     * @param string $parent    父id
     * @return array|mixed
     */
    public static function getTreeData($type='tree', $order='', $name='name', $child='id', $parent='pid')
    {
        // 判断是否需要排序
        if(empty($order)){
            $data = self::all([],'user')->toArray();
        }else{
            $data = self::all(function ($query) use ($order){
                $query->order($order,'desc');
            }, 'user')->toArray();
        }
        // 获取树形或者结构数据
        if($type == 'tree'){
            $data = Data::tree($data, $name, $child, $parent);
        }elseif($type = "level"){
            $data = Data::channelLevel($data, 0, '&nbsp;', $child);
        }
        return $data;
    }



}